//
//  WXLNetworktools.swift
//  WXLWeoBo
//
//  Created by 王晓雷 on 16/6/27.
//  Copyright © 2016年 王晓雷. All rights reserved.
//

import UIKit
import AFNetworking

// 请求方式枚举
enum MethodType: String {
    case GET = "GET"
    case POST = "POST"
}

class WXLNetworktools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools: WXLNetworktools = {
        let tools =  WXLNetworktools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    //
    // 给我们成功的闭包和失败的闭包格式起别名
    typealias SuccessClosure = (response: AnyObject?)->()
    typealias FailureClosure = (error: NSError)->()
    
    func request(method:MethodType, urlString: String, parameters: AnyObject?, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // GET 请求
        if method == .GET {
            self.GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
                    
            })
        }else {
            // POST 请求
            self.POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
                    
            })
        }
    }
}

extension  WXLNetworktools {
    // 请求token
    func oauthLoadUserAccount(code: String, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求url
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":REDIRECT_URI
        ]
        
        self.request(MethodType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    // 请求个人信息数据
    func oauthLoadUserInfo(userAccont:  WXLUserAccount, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        // 请求url
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":userAccont.access_token!,
            "uid":userAccont.uid!
        ]
        
        self.request(MethodType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
        
    }
}
    // 请求首页数据
    extension WXLNetworktools {
        func homeLoadStatusData(success: SuccessClosure, failure: FailureClosure){
            // 请求url
            let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
            // 请求参数
            // accessToken!  因为如果没有登录的话 根本就不会进行到请求数据 所以可以强行解包
            let params = ["access_token": WXLUserAccountTools.sharedTools.accessToken!]
            // 发送请求
            request(.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        }





}